<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<ng-container *ngIf="singleMode else multipleView">
  <div [formGroup]="valuesFormGroup" class="tb-form-panel no-border no-padding padding-top" fxLayout="column">
    <ng-container [ngTemplateOutlet]="singleView" [ngTemplateOutletContext]="{$implicit: null}"></ng-container>
  </div>
</ng-container>

<ng-template #multipleView>
  <mat-tab-group [formGroup]="valuesFormGroup">
    <mat-tab *ngFor="let register of modbusRegisterTypes" label="{{ ModbusValuesTranslationsMap.get(register) | translate }}">
      <div [formGroup]="valuesFormGroup.get(register)" class="tb-form-panel no-border no-padding padding-top" fxLayout="column">
        <ng-container [ngTemplateOutlet]="singleView" [ngTemplateOutletContext]="{$implicit: register}"></ng-container>
      </div>
    </mat-tab>
  </mat-tab-group>
</ng-template>

<ng-template #singleView let-register>
  <div class="tb-form-row space-between tb-flex">
    <div class="fixed-title-width" translate>gateway.attributes</div>
    <div class="tb-flex ellipsis-chips-container">
      <mat-chip-listbox [tb-ellipsis-chip-list]="getValueGroup(ModbusValueKey.ATTRIBUTES, register).value" class="tb-flex">
        <mat-chip *ngFor="let attribute of getValueGroup(ModbusValueKey.ATTRIBUTES, register).value">
          {{ attribute.tag }}
        </mat-chip>
        <mat-chip class="mat-mdc-chip ellipsis-chip">
          <label class="ellipsis-text"></label>
        </mat-chip>
      </mat-chip-listbox>
      <button type="button"
              mat-icon-button
              color="primary"
              [disabled]="disabled"
              #attributesButton
              (click)="manageKeys($event, attributesButton, ModbusValueKey.ATTRIBUTES, register)">
        <tb-icon matButtonIcon>edit</tb-icon>
      </button>
    </div>
  </div>
  <div class="tb-form-row space-between tb-flex">
    <div class="fixed-title-width" translate>gateway.timeseries</div>
    <div class="tb-flex ellipsis-chips-container">
      <mat-chip-listbox class="tb-flex" [tb-ellipsis-chip-list]="getValueGroup(ModbusValueKey.TIMESERIES, register).value">
        <mat-chip *ngFor="let telemetry of getValueGroup(ModbusValueKey.TIMESERIES, register).value">
          {{ telemetry.tag }}
        </mat-chip>
        <mat-chip class="mat-mdc-chip ellipsis-chip">
          <label class="ellipsis-text"></label>
        </mat-chip>
      </mat-chip-listbox>
      <button type="button"
              mat-icon-button
              color="primary"
              [disabled]="disabled"
              #telemetryButton
              (click)="manageKeys($event, telemetryButton, ModbusValueKey.TIMESERIES, register)">
        <tb-icon matButtonIcon>edit</tb-icon>
      </button>
    </div>
  </div>
  <div class="tb-form-row space-between tb-flex">
    <div class="fixed-title-width" translate>gateway.attribute-updates</div>
    <div class="tb-flex ellipsis-chips-container">
      <mat-chip-listbox [tb-ellipsis-chip-list]="getValueGroup(ModbusValueKey.ATTRIBUTES_UPDATES, register).value" class="tb-flex">
        <mat-chip *ngFor="let attributeUpdate of getValueGroup(ModbusValueKey.ATTRIBUTES_UPDATES, register).value">
          {{ attributeUpdate.tag }}
        </mat-chip>
        <mat-chip class="mat-mdc-chip ellipsis-chip">
          <label class="ellipsis-text"></label>
        </mat-chip>
      </mat-chip-listbox>
      <button type="button"
              mat-icon-button
              [disabled]="disabled"
              color="primary"
              #attributesUpdatesButton
              (click)="manageKeys($event, attributesUpdatesButton, ModbusValueKey.ATTRIBUTES_UPDATES, register)">
        <tb-icon matButtonIcon>edit</tb-icon>
      </button>
    </div>
  </div>
  <div class="tb-form-row space-between tb-flex">
    <div class="fixed-title-width" translate>gateway.rpc-requests</div>
    <div class="tb-flex ellipsis-chips-container">
      <mat-chip-listbox [tb-ellipsis-chip-list]="getValueGroup(ModbusValueKey.RPC_REQUESTS, register).value" class="tb-flex">
        <mat-chip *ngFor="let rpcRequest of getValueGroup(ModbusValueKey.RPC_REQUESTS, register).value">
          {{ rpcRequest.tag }}
        </mat-chip>
        <mat-chip class="mat-mdc-chip ellipsis-chip">
          <label class="ellipsis-text"></label>
        </mat-chip>
      </mat-chip-listbox>
      <button type="button"
              mat-icon-button
              color="primary"
              [disabled]="disabled"
              #rpcRequestsButton
              (click)="manageKeys($event, rpcRequestsButton, ModbusValueKey.RPC_REQUESTS, register)">
        <tb-icon matButtonIcon>edit</tb-icon>
      </button>
    </div>
  </div>
</ng-template>

